<!-- 友链编辑弹窗 -->
<template>
  <el-dialog :title="isUpdate ? '修改热搜词' : '添加热搜词'" :visible="visible" width="1060px" :destroy-on-close="true"
    :lock-scroll="false" :close-on-click-modal="false" :before-close="confirmClose" @update:visible="updateVisible">
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">
      <el-row :gutter="15">
        <el-col :sm="12">
          <el-form-item label="热搜词:" prop="name">
            <el-input clearable :maxlength="20" v-model="form.name" placeholder="请输入热门搜索词" />
          </el-form-item>
          <el-form-item label="使用场景:" prop="type">
            <el-select clearable class="ele-block" v-model="form.scene" placeholder="请选择友链类型">
              <el-option label="游戏搜索" :value="1" />
              <el-option label="社区搜索" :value="2" />
            </el-select>
          </el-form-item>
          <el-form-item label="排序号:" prop="sort">
            <el-input-number :min="0" v-model="form.sort" placeholder="请输入排序号" controls-position="right"
              class="ele-fluid ele-text-left" />
          </el-form-item>
        </el-col>
        <el-col :sm="12">
          <el-form-item label="投放平台:" prop="device">
            <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll"
              @change="handleCheckAllChange">全选</el-checkbox>
            <div style="margin: 15px 0;"></div>
            <el-checkbox-group v-model="checkedDevice" @change="handleCheckedDeviceChange" border>
              <el-checkbox v-for="item in arrDevice" :label="item.value" :key="item.value">{{ item.label
                }}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="状态:" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="2">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="备注:" style="margin-top: 20px">
        <el-input :rows="3" clearable type="textarea" :maxlength="200" v-model="form.desc" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="confirmClose()">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>

export default {
  name: 'LinkEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({ form: 1, status: 1, scene: 1 }, this.data),
      // 表单验证规则
      rules: {
        name: [
          { required: true, message: '请输入热门搜索词', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择热门搜索词状态', trigger: 'blur' }
        ],
        device: [
          { required: true, message: '请选择投放设备', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入排序号', trigger: 'blur' }
        ]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      isIndeterminate: true,
      checkAll: false,
      checkedDevice: [],
      arrDevice: [
        {
          label: "安卓",
          value: 2
        },
        {
          label: "IOS",
          value: 3
        },
        {
          label: "PAD",
          value: 4
        },
        {
          label: "IPAD",
          value: 5
        },
        {
          label: "PC",
          value: 6
        },
      ],
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
        if (this.form.device) {
          if (this.form.device.includes('1')) {
            this.handleCheckAllChange(true)
            this.checkAll = true
          } else {
            if (this.form.device == '') {
              this.checkAll = false
              this.checkedDevice = []
            } else {
              this.checkAll = false
              this.form.device = this.form.device.split(',')
              this.checkedDevice = this.form.device.map(Number)
            }
          }
        }
      } else {
        this.isUpdate = false;
        this.form = {};
        this.checkedDevice = [];
        this.checkAll = false
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/BoxHotKeys/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },

    confirmClose() {
      this.$confirm('确定要返回吗？', '温馨提示').then(() => {
        this.updateVisible(false)
        if (!this.isUpdate) {
          this.form = {};
          this.checkedDevice = []
          this.checkAll = false
        }
      }).catch(() => {

      });
    },

    handleCheckAllChange(val) {
      this.checkedDevice = val ? [2, 3, 4, 5, 6] : [];
      this.isIndeterminate = false;
      if (val) {
        this.form.device = 1;
      } else {
        this.form.device = ''
      }
    },

    handleCheckedDeviceChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.arrDevice.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.arrDevice.length;
      if (this.checkAll) {
        this.form.device = 1;
      } else {
        this.form.device = value.join(',');
      }
    }
  }
}
</script>

<style scoped></style>
